package se.webbzon.boltzmann.player;

public class PlayerHealth {
	
	// The maximal top health of a player
	private static final int topHealth = 104;
	
	// The number of fractions per health unit
	private static final int fractionsPerUnit = 8;
	
	// The current maximal health of the player
	private int maxHealth;
	
	// The number of whole units and fractions of the players maximal health
	private int maxUnits, maxFractions;
	
	// The current health of the player
	private int health;
	
	// The number of whole units and fractions of the current health
	private int units, fractions;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new player health instance with a maximal health of 
	 * 8 pieces. **/
	public PlayerHealth() {
		this(8,8);
	}
	
	/** Creates a new player health instance with a given health and
	 * maximal health. **/
	public PlayerHealth(int maxHealth, int health) {
		this.maxHealth = (maxHealth > topHealth) ? topHealth : maxHealth;
		this.health = (health > this.maxHealth) ? this.maxHealth : health;
		updateHealth(this.health);
		updateMaxHealth(this.maxHealth);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the health of the player. **/
	public int getHealth() {
		return health;
	}

	/** Sets the health of the player. **/
	public void setHealth(int health) {
		this.health = (health > maxHealth) ? maxHealth : health;
		updateHealth(this.health);
	}
	
	/** Returns the maximal health of the player. **/
	public int getMaxHealth() {
		return maxHealth;
	}
	
	public void setMaxHealth(int maxHealth) {
		this.maxHealth = (maxHealth > topHealth) ? topHealth : maxHealth;
		updateMaxHealth(this.maxHealth);
	}
	
	/** Returns the number of whole health units. **/
	public int getUnits() {
		return units;
	}
	
	/** Returns the maximal number of whole health units. **/
	public int getMaxUnits() {
		return maxUnits;
	}
	
	/** Returns the number of health unit fractions. **/
	public int getFractions() {
		return fractions;
	}
	
	/** Returns the maximal number of health unit fractions. **/
	public int getMaxFractions() {
		return maxFractions;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Updates the whole units and fractions of the players health. **/
	private void updateHealth(int health) {
		this.units = health / fractionsPerUnit;
		this.fractions = health % fractionsPerUnit;
	}
	
	/** Updates the whole units and fractions of the players maximal health. **/
	private void updateMaxHealth(int maxHealth) {
		this.maxUnits = maxHealth / fractionsPerUnit;
		this.maxFractions = maxHealth % fractionsPerUnit;
	}

}
